<template>
  <div class="result-display-container">
    <h2 class="result-title">抽中结果</h2>
    <div class="result-content">
      <div 
        class="food-result"
        :class="{ 'has-result': currentFood, 'rolling': isRolling }"
      >
        <div v-if="!currentFood" class="no-result">
          <p>点击按钮开始抽取</p>
        </div>
        <div v-else class="food-result-inner">
          <div class="food-name-wrapper">
            <span class="food-name">{{ displayFoodName }}</span>
          </div>
          <div class="food-category-wrapper">
            <span class="food-category">{{ isRolling ? '抽取中...' : getCategoryName(currentFood.category) }}</span>
          </div>
        </div>
        
        <!-- 抽取中的动态指示器 -->
        <div v-if="isRolling" class="rolling-indicators">
          <div class="indicator" v-for="i in 3" :key="i" :style="{ animationDelay: `${i * 0.2}s` }"></div>
        </div>
      </div>
      
      <div v-if="currentFood" class="celebration">
        <div class="confetti" v-for="i in 20" :key="i" :style="getConfettiStyle(i)"></div>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  name: 'ResultDisplay',
  props: {
    currentFood: {
      type: Object,
      default: null
    },
    isRolling: {
      type: Boolean,
      default: false
    },
    rollingFoods: {
      type: Array,
      default: () => []
    }
  },
  data() {
    return {
      displayFoodName: '',
      currentIndex: 0
    };
  },
  watch: {
    isRolling(newVal) {
      if (newVal && this.rollingFoods.length > 0) {
        this.startRolling();
      } else {
        this.stopRolling();
      }
    },
    currentFood(newFood) {
      if (newFood && !this.isRolling) {
        this.displayFoodName = newFood.name;
      }
    }
  },
  methods: {
    getCategoryName(category) {
      const categoryMap = {
        'chinese': '中餐',
        'western': '西餐',
        'japanese': '日料',
        'korean': '韩餐',
        'fastfood': '快餐',
        'snack': '小吃',
        'dessert': '甜品',
        'drink': '饮品'
      };
      return categoryMap[category] || category;
    },
    startRolling() {
      this.currentIndex = 0;
      // 滚动速度逐渐加快，然后在结束前减速
      let speed = 100;
      const acceleration = 2;
      
      this.rollingInterval = setInterval(() => {
        this.displayFoodName = this.rollingFoods[this.currentIndex].name;
        this.currentIndex = (this.currentIndex + 1) % this.rollingFoods.length;
        
        // 逐渐加速
        if (speed > 30) {
          speed -= acceleration;
          clearInterval(this.rollingInterval);
          // 直接设置新的间隔，而不是再次调用startRolling
          this.rollingInterval = setInterval(() => {
            this.displayFoodName = this.rollingFoods[this.currentIndex].name;
            this.currentIndex = (this.currentIndex + 1) % this.rollingFoods.length;
          }, speed);
        }
      }, speed);
    },
    stopRolling() {
      if (this.rollingInterval) {
        clearInterval(this.rollingInterval);
      }
      if (this.currentFood) {
        this.displayFoodName = this.currentFood.name;
      }
    },
    getConfettiStyle(index) {
      const colors = ['#FF5252', '#FF4081', '#E040FB', '#7C4DFF', '#536DFE', '#448AFF', '#40C4FF', '#18FFFF', '#64FFDA', '#69F0AE', '#B2FF59', '#EEFF41', '#FFFF00', '#FFD740', '#FFAB40', '#FF6E40'];
      const size = Math.random() * 10 + 5;
      const color = colors[index % colors.length];
      const left = Math.random() * 100;
      const animationDuration = Math.random() * 3 + 2;
      const animationDelay = Math.random() * 2;
      const rotation = Math.random() * 360;
      
      return {
        width: `${size}px`,
        height: `${size}px`,
        backgroundColor: color,
        left: `${left}%`,
        animationDuration: `${animationDuration}s`,
        animationDelay: `${animationDelay}s`,
        transform: `rotate(${rotation}deg)`
      };
    }
  },
  beforeUnmount() {
    this.stopRolling();
  }
}
</script>

<style scoped>
.result-display-container {
  background: white;
  border-radius: 12px;
  padding: 24px;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
  text-align: center;
}

.result-title {
  margin: 0 0 20px 0;
  font-size: 20px;
  font-weight: 600;
  color: #333;
}

.result-content {
  position: relative;
  min-height: 200px;
}

.food-result {
  position: relative;
  background: linear-gradient(135deg, #f5f7fa 0%, #c3cfe2 100%);
  border-radius: 16px;
  padding: 40px 20px;
  transition: all 0.3s ease;
  min-height: 150px;
  display: flex;
  align-items: center;
  justify-content: center;
}

.food-result.has-result {
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  box-shadow: 0 10px 30px rgba(102, 126, 234, 0.4);
}

.food-result.rolling {
  animation: pulse 0.5s ease-in-out infinite, glow 1.5s ease-in-out infinite;
  border: 3px solid transparent;
  background-clip: padding-box, border-box;
  background-origin: padding-box, border-box;
  background-image: 
    linear-gradient(135deg, #667eea 0%, #764ba2 100%),
    linear-gradient(90deg, #667eea, #764ba2, #f093fb, #f5576c, #667eea);
  background-size: 100% 100%, 300% 100%;
  animation: 
    pulse 0.5s ease-in-out infinite,
    glow 1.5s ease-in-out infinite,
    rotateBorder 2s linear infinite;
}

@keyframes pulse {
  0%, 100% {
    transform: scale(1);
  }
  50% {
    transform: scale(1.02);
  }
}

@keyframes glow {
  0%, 100% {
    box-shadow: 0 10px 30px rgba(102, 126, 234, 0.4);
  }
  50% {
    box-shadow: 0 10px 40px rgba(102, 126, 234, 0.7), 0 0 20px rgba(240, 147, 251, 0.4);
  }
}

@keyframes rotateBorder {
  0% {
    background-position: 100% 100%, 0% 100%;
  }
  100% {
    background-position: 100% 100%, 100% 100%;
  }
}

.no-result {
  color: #999;
  font-size: 16px;
}

.food-result-inner {
  color: white;
  text-align: center;
}

.food-name-wrapper {
  margin-bottom: 16px;
}

.food-name {
  font-size: 32px;
  font-weight: 700;
  text-shadow: 0 2px 4px rgba(0, 0, 0, 0.2);
  transition: all 0.3s ease;
  display: inline-block;
}

.food-result.rolling .food-name {
  animation: textBounce 0.5s ease-in-out infinite, textGlow 1.5s ease-in-out infinite;
}

@keyframes textBounce {
  0%, 100% {
    transform: translateY(0);
  }
  50% {
    transform: translateY(-5px);
  }
}

@keyframes textGlow {
  0%, 100% {
    text-shadow: 0 2px 4px rgba(0, 0, 0, 0.2);
  }
  50% {
    text-shadow: 
      0 2px 4px rgba(0, 0, 0, 0.2),
      0 0 10px rgba(255, 255, 255, 0.8),
      0 0 20px rgba(102, 126, 234, 0.6);
  }
}

.food-category-wrapper {
  display: inline-block;
}

.food-category {
  background: rgba(255, 255, 255, 0.2);
  padding: 6px 16px;
  border-radius: 20px;
  font-size: 14px;
  font-weight: 500;
}

.celebration {
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  pointer-events: none;
  overflow: hidden;
}

.confetti {
  position: absolute;
  top: -20px;
  opacity: 0;
  animation-name: fall;
  animation-timing-function: ease-in-out;
  animation-fill-mode: forwards;
}

@keyframes fall {
  0% {
    top: -20px;
    opacity: 0;
    transform: translateY(0) rotate(0deg);
  }
  10% {
    opacity: 1;
  }
  90% {
    opacity: 1;
  }
  100% {
    top: 100%;
    opacity: 0;
    transform: translateY(0) rotate(360deg);
  }
}

/* 抽取中的指示器样式 */
.rolling-indicators {
  position: absolute;
  bottom: 20px;
  left: 50%;
  transform: translateX(-50%);
  display: flex;
  gap: 8px;
}

.indicator {
  width: 8px;
  height: 8px;
  background: white;
  border-radius: 50%;
  animation: indicatorPulse 1.5s ease-in-out infinite;
}

@keyframes indicatorPulse {
  0%, 100% {
    transform: scale(0.8);
    opacity: 0.6;
  }
  50% {
    transform: scale(1.2);
    opacity: 1;
    background: #fff;
    box-shadow: 0 0 10px rgba(255, 255, 255, 0.8);
  }
}

/* 响应式设计 */
@media (max-width: 768px) {
  .result-display-container {
    padding: 20px;
  }
  
  .result-title {
    font-size: 18px;
  }
  
  .food-result {
    padding: 30px 16px;
    min-height: 120px;
    border-width: 2px;
  }
  
  .food-name {
    font-size: 24px;
  }
  
  .food-category {
    padding: 4px 12px;
    font-size: 12px;
  }
  
  .rolling-indicators {
    bottom: 16px;
    gap: 6px;
  }
  
  .indicator {
    width: 6px;
    height: 6px;
  }
}

@media (max-width: 480px) {
  .result-display-container {
    padding: 16px;
  }
  
  .result-title {
    font-size: 16px;
  }
  
  .food-result {
    padding: 24px 12px;
    min-height: 100px;
  }
  
  .food-name {
    font-size: 20px;
  }
}
</style>